﻿using System.Configuration;
using System.Security;
using System.Security.Cryptography;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Diagnostics;

using Tipler.OzellikTipleri;
using DataServis;
using Tipler.DataSetler;
using Tipler.Enumlar;

public class VeriSQL : VeriArabirim
{

    private SqlConnection SqlBaglantisi;
    private SqlTransaction Transaction;
    private bool TransactionalMi;

    private Nullable<Guid> KullaniciID;

    public bool OzellikTransactionalMi
    {
        get { return TransactionalMi; }
    }

    public string VeritabaniAdi
    {
        get
        {
            string Sonuc = "";
            try
            {
                BaglantiyiAc();
                Sonuc += SqlBaglantisi.Database;
                Sonuc += ";" + SqlBaglantisi.DataSource;
            }
            catch (Exception)
            {
            }
            finally
            {
                BaglantiyiKapat();
            }
            return Sonuc;
        }
    }

    public VeriSQL(string cnStr, bool transactionGerekiyorMu)
        : this(cnStr, transactionGerekiyorMu, "")
    {
    }

    public VeriSQL(string cnStr, string pKullaniciID)
        : this(cnStr, false, pKullaniciID) { }


    public VeriSQL(string cnStr, bool transactionGerekiyorMu, string pKullaniciID)
    {
        if (string.IsNullOrEmpty(pKullaniciID))
        {
            KullaniciID = null;
        }
        else
        {
            KullaniciID = new Guid(pKullaniciID);
        }


        SqlBaglantisi = new SqlConnection(cnStr);
        TransactionalMi = transactionGerekiyorMu;
        if (TransactionalMi)
        {
            SqlBaglantisi.Open();
            TransactionBaslat();
        }
    }


    /*
    //' Farkli veritabanlarına baglanti constructor'ı
    public Veri(bool transactionGerekiyorMu, string dbConfigName, string pKullaniciID)
    {
        //'sCnstr = BaglantiCumlesiIslemcisi.GetConnectionString(sDBConfigName)
        //If String.IsNullOrEmpty(BaglantiCumlesi) Then
        if (string.IsNullOrEmpty(pKullaniciID)) {
            KullaniciID = null;
        }
        else {
            KullaniciID = new Guid(pKullaniciID);
        }
        
        #if DEBUG
        switch (dbConfigName) {
            case "demoserver1":
        BaglantiCumlesi = "server=demoserver;initial catalog=dbname1;user id=user1;password=pass1;";
                break;
            case "demoserver2":
        BaglantiCumlesi = "server=demoserver;initial catalog=dbname1;user id=user1;password=pass1;";
                break;
        }
        #else
        switch (dbConfigName) {
            case "productionserver1":
                BaglantiCumlesi = "server=productionservername1;initial catalog=dbname2;user id=user2;password=pass2;";
                break;
            case "productionserver2":
                BaglantiCumlesi = "server=productionservername2;initial catalog=dbname2;user id=user2;password=pass2;";
                break;
        }
        #endif
        //End If
         
        SqlBaglantisi = new SqlConnection(BaglantiCumlesi);
        TransactionalMi = transactionGerekiyorMu;
        if (TransactionalMi) {
            SqlBaglantisi.Open();
            TransactionBaslat();
        }
    }
    */

    private void TransactionBaslat()
    {
        Transaction = SqlBaglantisi.BeginTransaction();
    }

    public void TransactionGeriAl()
    {
        if (TransactionalMi)
        {
            Transaction.Rollback();
            SqlBaglantisi.Close();
        }
    }

    public void TransactionBitir()
    {
        if (TransactionalMi)
        {
            Transaction.Commit();
            SqlBaglantisi.Close();
        }
    }

    public int SQLKomutCalistir(string sqlCumlesi)
    {
        int functionReturnValue = 0;
        try
        {
            BaglantiyiAc();
            using (SqlCommand SqlKomutu = new SqlCommand(sqlCumlesi, SqlBaglantisi))
            {
                if (TransactionalMi) SqlKomutu.Transaction = Transaction;
                functionReturnValue = SqlKomutu.ExecuteNonQuery();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            BaglantiyiKapat();
        }
        return functionReturnValue;
    }

    public int SQLKomutCalistir(string sqlCumlesi, List<IDbDataParameter> paramaters)
    {
        int functionReturnValue = 0;
        BaglantiyiAc();
        try
        {
            using (SqlCommand SqlKomutu = new SqlCommand(sqlCumlesi, SqlBaglantisi))
            {
                if (TransactionalMi) SqlKomutu.Transaction = Transaction;
                SqlKomutu.Parameters.AddRange(paramaters.ToArray());
                functionReturnValue = SqlKomutu.ExecuteNonQuery();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            BaglantiyiKapat();
        }
        return functionReturnValue;
    }





    //public void VeriGetir(DataSet dataSet, string sqlCumlesi)
    //{
    //    if (dataSet == null)
    //        dataSet = new DataSet();
    //    if (dataSet.Tables.Count == 0) dataSet.Tables.Add();
    //    DataTable oDataTable = dataSet.Tables[0];
    //    VeriGetir(ref oDataTable, sqlCumlesi);
    //}

    //public void VeriGetir(DataSet dataSet, string sqlCumlesi, List<IDbDataParameter> @params)
    //{
    //    if (dataSet.Tables.Count == 0) dataSet.Tables.Add();
    //    DataTable oDataTable = dataSet.Tables[0];
    //    VeriGetir(ref oDataTable, sqlCumlesi, @params);
    //}

    //public void VeriGetir(ref DataSet dataSet, string sqlCumlesi, string TabloAdi)
    //{
    //    DataTable oDataTable = dataSet.Tables[TabloAdi];
    //    VeriGetir(ref oDataTable, sqlCumlesi);
    //}

    //public DataSet VeriGetir(string sqlCumlesi)
    //{
    //    DataSet DataSetReturn = new DataSet();
    //    DataSetReturn.Tables.Add(new DataTable());
    //    VeriGetir(DataSetReturn, sqlCumlesi);
    //    return DataSetReturn;
    //}

    //public DataSet VeriGetir(string sqlCumlesi, List<IDbDataParameter> @params)
    //{
    //    DataSet DataSetReturn = new DataSet();
    //    DataSetReturn.Tables.Add(new DataTable());
    //    VeriGetir(DataSetReturn, sqlCumlesi, @params);
    //    return DataSetReturn;
    //}

    //private void VeriGetir(ref DataTable dataTable, string sqlCumlesi)
    //{
    //    try
    //    {
    //        BaglantiyiAc();
    //        if ((KullaniciID != null)) LogReadDataTable(dataTable, sqlCumlesi);
    //        using (SqlDataAdapter DataAdapter = new SqlDataAdapter(sqlCumlesi, SqlBaglantisi))
    //        {
    //            if (TransactionalMi) DataAdapter.SelectCommand.Transaction = Transaction;

    //            DataAdapter.Fill(dataTable);
    //        }
    //    }
    //    catch (Exception ex)
    //    {
    //        throw ex;
    //    }
    //    finally
    //    {
    //        BaglantiyiKapat();
    //    }
    //}

    //private void VeriGetir(ref DataTable dataTable, string sqlCumlesi, List<IDbDataParameter> @params)
    //{
    //    try
    //    {
    //        BaglantiyiAc();
    //        if ((KullaniciID != null)) LogReadDataTable(dataTable, sqlCumlesi);

    //        using (SqlDataAdapter DataAdapter = new SqlDataAdapter(sqlCumlesi, SqlBaglantisi))
    //        {
    //            if (TransactionalMi) DataAdapter.SelectCommand.Transaction = Transaction;
    //            DataAdapter.SelectCommand.Parameters.AddRange(@params.ToArray());
    //            DataAdapter.Fill(dataTable);
    //        }
    //    }
    //    catch (Exception ex)
    //    {
    //        throw ex;
    //    }
    //    finally
    //    {
    //        BaglantiyiKapat();
    //    }
    //}

    public void VeriGetir(DataSet dataSet, string sqlCumlesi)
    {
        VeriGetir(dataSet, sqlCumlesi, null, 0, false);
    }

    public void VeriGetir(DataSet dataSet, string sqlCumlesi, List<IDbDataParameter> @params)
    {
        VeriGetir(dataSet, sqlCumlesi, @params, 0, false);
    }

    public void VeriGetir(DataSet dataSet, string sqlCumlesi, int nMaxKayitSayisi)
    {
        VeriGetir(dataSet, sqlCumlesi, null, nMaxKayitSayisi, false);
    }

    public void VeriGetir(DataSet dataSet, string sqlCumlesi, bool longExecutionTime)
    {
        VeriGetir(dataSet, sqlCumlesi, null, 0, longExecutionTime);
    }

    public void VeriGetir(DataSet dataSet, string sqlCumlesi, List<IDbDataParameter> @params, int nMaxKayitSayisi, bool longExecutionTime)
    {
        try
        {
            if (dataSet == null)
                dataSet = new DataSet();
            if (dataSet.Tables.Count == 0)
                dataSet.Tables.Add();

            BaglantiyiAc();
            LogReadDataSet(dataSet);

            using (SqlDataAdapter DataAdapter = new SqlDataAdapter(sqlCumlesi, SqlBaglantisi))
            {
                if (longExecutionTime)
                    DataAdapter.SelectCommand.CommandTimeout = 500;
                if (TransactionalMi) DataAdapter.SelectCommand.Transaction = Transaction;
                if (@params != null)
                    DataAdapter.SelectCommand.Parameters.AddRange(@params.ToArray());
                DataAdapter.Fill(dataSet, 0, nMaxKayitSayisi, dataSet.Tables[0].TableName);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            BaglantiyiKapat();
        }
    }



    public int TopluVeriIsleme(DataSet dataSet, string sqlCumlesi, string TabloAdi)
    {
        return TopluVeriIsleme(dataSet.Tables[TabloAdi], sqlCumlesi);
    }

    public int TopluVeriIsleme(DataSet dataSet, string sqlCumlesi)
    {
        return TopluVeriIsleme(dataSet.Tables[0], sqlCumlesi);
    }

    public int TopluVeriIsleme(DataSet dataSet)
    {
        return TopluVeriIsleme(dataSet.Tables[0], "SELECT * FROM " + dataSet.Tables[0].TableName);
    }

    public int TopluVeriIsleme(DataTable dataTable)
    {
        return TopluVeriIsleme(dataTable, "SELECT * FROM " + dataTable.TableName);
    }

    public int TopluVeriIsleme(DataTable dataTable, string sqlCumlesi)
    {
        int functionReturnValue = 0;
        try
        {
            BaglantiyiAc();
            using (SqlDataAdapter DataAdapter = new SqlDataAdapter(sqlCumlesi, SqlBaglantisi))
            {
                if (TransactionalMi) DataAdapter.SelectCommand.Transaction = Transaction;
                using (SqlCommandBuilder CommandBuilder = new SqlCommandBuilder(DataAdapter))
                {
                    LogWriteDataTable(dataTable);
                    functionReturnValue = DataAdapter.Update(dataTable);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            BaglantiyiKapat();
        }
        return functionReturnValue;
    }


    public object TekDegerGetir(string sqlCumlesi)
    {
        try
        {
            BaglantiyiAc();
            using (SqlCommand SqlKomutu = new SqlCommand(sqlCumlesi, SqlBaglantisi))
            {
                if (TransactionalMi) SqlKomutu.Transaction = Transaction;
                return (SqlKomutu.ExecuteScalar());
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            BaglantiyiKapat();
        }
    }

    public object TekDegerGetir(string sqlCumlesi, List<IDbDataParameter> @params)
    {
        try
        {
            BaglantiyiAc();
            using (SqlCommand SqlKomutu = new SqlCommand(sqlCumlesi, SqlBaglantisi))
            {
                if (TransactionalMi) SqlKomutu.Transaction = Transaction;
                SqlKomutu.Parameters.AddRange(@params.ToArray());
                return (SqlKomutu.ExecuteScalar());
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            BaglantiyiKapat();
        }
    }


    private void BaglantiyiAc()
    {
        if (!TransactionalMi)
        {
            if (SqlBaglantisi.State != ConnectionState.Open)
                SqlBaglantisi.Open();
        }
    }

    private void BaglantiyiKapat()
    {
        if (!TransactionalMi)
        {
            SqlBaglantisi.Close();
        }
    }


    private void LogReadDataSet(DataSet ds)
    {
        if (ds != null)
        {
            if (ds.GetType().IsDefined(typeof(LogDataTable), true))
            {
                LogDataTable att = (LogDataTable)ds.GetType().GetCustomAttributes(typeof(LogDataTable), true)[0];
                if (att.EnableReadLog)
                {

                    string FonksiyonAdi = GetCallerName();

                    int nKullaniciID;

                    string sIpAdres = "";

                    object oKullaniciID = System.Threading.Thread.GetData(System.Threading.Thread.GetNamedDataSlot("KullaniciID"));

                    if (oKullaniciID != null)
                        nKullaniciID = Convert.ToInt32(oKullaniciID);
                    else
                        return;

                    object oIPAdres = System.Threading.Thread.GetData(System.Threading.Thread.GetNamedDataSlot("IP"));

                    if (oIPAdres != null)
                        sIpAdres = oIPAdres.ToString();

                    LogDataSet dsLog = new LogDataSet();
                    LogDataSet.LogRow drLog = dsLog.Log.NewLogRow();

                    drLog.KullaniciID = nKullaniciID;
                    drLog.TabloAdi = ds.Tables[0].TableName.ToUpper();
                    drLog.ProsedurAdi = FonksiyonAdi;
                    drLog.IslemTuru = Log_Yapilan_Islem_Turu.Listeleme.GetHashCode().ToString();
                    drLog.IslemTarihi = DateTime.Now;
                    drLog.AnahtarAlanlar = "";
                    drLog.IPAdresi = sIpAdres;
                    dsLog.Log.AddLogRow(drLog);

                    try
                    {
                        SqlDataAdapter da = new SqlDataAdapter("Select * from " + dsLog.Log.TableName, SqlBaglantisi);
                        if (this.TransactionalMi) da.SelectCommand.Transaction = Transaction;
                        SqlCommandBuilder builder1 = new SqlCommandBuilder(da);
                        da.Update(dsLog.Log);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
        }
    }

    private void LogReadDataTable(DataTable dt, string ssql)
    {
        if ((dt != null) && (dt.DataSet != null))
        {
            if (dt.DataSet.GetType().IsDefined(typeof(LogDataTable), true))
            {
                LogDataTable att = (LogDataTable)dt.DataSet.GetType().GetCustomAttributes(typeof(LogDataTable), true)[0];
                if (att.EnableReadLog)
                {

                    string FonksiyonAdi = GetCallerName();
                    int nKullaniciID;

                    string sIpAdres = "";

                    object oKullaniciID = System.Threading.Thread.GetData(System.Threading.Thread.GetNamedDataSlot("KullaniciID"));

                    if (oKullaniciID != null)
                        nKullaniciID = Convert.ToInt32(oKullaniciID);
                    else
                        return;

                    object oIPAdres = System.Threading.Thread.GetData(System.Threading.Thread.GetNamedDataSlot("IP"));

                    if (oIPAdres != null)
                        sIpAdres = oIPAdres.ToString();

                    LogDataSet dsLog = new LogDataSet();
                    LogDataSet.LogRow drLog = dsLog.Log.NewLogRow();

                    drLog.KullaniciID = nKullaniciID;
                    drLog.TabloAdi = dt.TableName.ToUpper();
                    drLog.ProsedurAdi = FonksiyonAdi;
                    drLog.IslemTuru = Log_Yapilan_Islem_Turu.Listeleme.GetHashCode().ToString();
                    drLog.IslemTarihi = DateTime.Now;
                    drLog.AnahtarAlanlar = "";
                    drLog.IPAdresi = sIpAdres;
                    dsLog.Log.AddLogRow(drLog);

                    try
                    {
                        SqlDataAdapter da = new SqlDataAdapter("Select * from " + dsLog.Log.TableName, SqlBaglantisi);
                        if (this.TransactionalMi) da.SelectCommand.Transaction = Transaction;
                        SqlCommandBuilder builder1 = new SqlCommandBuilder(da);
                        da.Update(dsLog.Log);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
        }
    }

    private void LogWriteDataTable(DataTable dt)
    {
        if (dt.DataSet.GetType().IsDefined(typeof(LogDataTable), true))
        {
            LogDataTable att = (LogDataTable)dt.DataSet.GetType().GetCustomAttributes(typeof(LogDataTable), true)[0];
            if (att.EnableWriteLog)
            {

                try
                {

                    foreach (DataRow dr in dt.Rows)
                    {
                        if (dr.RowState == DataRowState.Added | dr.RowState == DataRowState.Deleted | dr.RowState == DataRowState.Modified)
                        {

                            string sOrjinal = "";
                            string sYeni = "";
                            string sIslemTuru = "";
                            string sAnahtarAlanlar = "";
                            bool bLogYaz = true;

                            switch (dr.RowState)
                            {
                                case DataRowState.Added:
                                    sIslemTuru = Log_Yapilan_Islem_Turu.Yeni_Kayıt.GetHashCode().ToString();
                                    foreach (DataColumn column in dt.PrimaryKey)
                                    {
                                        sAnahtarAlanlar += column.ColumnName + "=" + dr[column.ColumnName].ToString() + "+";
                                    }

                                    foreach (DataColumn column in dt.Columns)
                                    {
                                        sYeni += column.ColumnName + "=" + dr[column.ColumnName].ToString() + "+";
                                    }

                                    sYeni = sYeni.Substring(0, sYeni.Length - 1);
                                    if (sAnahtarAlanlar.Length != 0)
                                    {
                                        sAnahtarAlanlar = sAnahtarAlanlar.Substring(0, sAnahtarAlanlar.Length - 1);
                                    }

                                    break;
                                case DataRowState.Deleted:
                                    sIslemTuru = Log_Yapilan_Islem_Turu.Kayıt_Silme.GetHashCode().ToString();
                                    foreach (DataColumn column in dt.PrimaryKey)
                                    {
                                        sAnahtarAlanlar += column.ColumnName + "=" + dr[column.ColumnName, DataRowVersion.Original].ToString() + "+";
                                    }

                                    foreach (DataColumn column in dt.Columns)
                                    {
                                        sOrjinal += column.ColumnName + "=" + dr[column.ColumnName, DataRowVersion.Original].ToString() + "+";
                                    }

                                    sOrjinal = sOrjinal.Substring(0, sOrjinal.Length - 1);
                                    if (sAnahtarAlanlar.Length != 0)
                                    {
                                        sAnahtarAlanlar = sAnahtarAlanlar.Substring(0, sAnahtarAlanlar.Length - 1);
                                    }


                                    break;
                                case DataRowState.Modified:
                                    sIslemTuru = Log_Yapilan_Islem_Turu.Kayıt_Güncelleme.GetHashCode().ToString();
                                    foreach (DataColumn column in dt.PrimaryKey)
                                    {
                                        sAnahtarAlanlar += column.ColumnName + "=" + dr[column.ColumnName].ToString() + "+";
                                    }

                                    foreach (DataColumn column in dt.Columns)
                                    {
                                        if (dr[column.ColumnName].ToString() != dr[column.ColumnName, DataRowVersion.Original].ToString())
                                        {
                                            sYeni += column.ColumnName + "=" + dr[column.ColumnName].ToString() + "+";
                                            sOrjinal += column.ColumnName + "=" + dr[column.ColumnName, DataRowVersion.Original].ToString() + "+";
                                        }
                                    }

                                    if (string.IsNullOrEmpty(sYeni) & string.IsNullOrEmpty(sOrjinal))
                                    {
                                        bLogYaz = false;
                                    }
                                    else
                                    {
                                        sYeni = sYeni.Substring(0, sYeni.Length - 1);
                                        sOrjinal = sOrjinal.Substring(0, sOrjinal.Length - 1);
                                        if (sAnahtarAlanlar.Length != 0)
                                        {
                                            sAnahtarAlanlar = sAnahtarAlanlar.Substring(0, sAnahtarAlanlar.Length - 1);
                                        }
                                    }

                                    break;
                            }

                            if (bLogYaz)
                            {

                                string FonksiyonAdi = GetCallerName();

                                int nKullaniciID;

                                string sIpAdres = "";

                                object oKullaniciID = System.Threading.Thread.GetData(System.Threading.Thread.GetNamedDataSlot("KullaniciID"));

                                if (oKullaniciID != null)
                                    nKullaniciID = Convert.ToInt32(oKullaniciID);
                                else
                                    return;

                                object oIPAdres = System.Threading.Thread.GetData(System.Threading.Thread.GetNamedDataSlot("IP"));

                                if (oIPAdres != null)
                                    sIpAdres = oIPAdres.ToString();

                                LogDataSet dsLog = new LogDataSet();
                                LogDataSet.LogRow drLog = dsLog.Log.NewLogRow();

                                drLog.KullaniciID = nKullaniciID;
                                drLog.TabloAdi = dt.TableName.ToUpper();
                                drLog.ProsedurAdi = FonksiyonAdi;
                                drLog.IslemTuru = sIslemTuru;
                                drLog.IslemTarihi = DateTime.Now;
                                drLog.AnahtarAlanlar = sAnahtarAlanlar;
                                drLog.IPAdresi = sIpAdres;

                                if (sYeni != string.Empty) drLog.YeniVeri = sYeni; else drLog.SetYeniVeriNull();
                                if (sOrjinal != string.Empty) drLog.OrjinalVeri = sOrjinal; else drLog.SetOrjinalVeriNull();
                                dsLog.Log.AddLogRow(drLog);

                                try
                                {
                                    SqlDataAdapter da = new SqlDataAdapter("Select * from " + dsLog.Log.TableName, SqlBaglantisi);
                                    if (this.TransactionalMi) da.SelectCommand.Transaction = Transaction;
                                    SqlCommandBuilder builder1 = new SqlCommandBuilder(da);
                                    da.Update(dsLog.Log);
                                }
                                catch (Exception ex)
                                {

                                    throw ex;

                                }
                            }
                        }

                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
    }

    private string GetCallerName()
    {
        StackTrace st = new StackTrace(true);
        StackFrame sf = null;
        int i = 0;

        for (i = 0; i <= st.FrameCount - 1; i++)
        {
            sf = st.GetFrame(i);
            if (sf.GetMethod().DeclaringType.FullName.Contains("IsKurallari"))
            {
                return sf.GetMethod().DeclaringType.FullName + "." + sf.GetMethod().Name;
            }
        }
        return "";
    }



    /// Primary Key'lar TableName+"ID" formatına uygun yapıda ise 
    /// bu fonksiyonu kullanabilirsiniz. 
    /// </summary>
    /// <param name="ds"></param>
    public void TopluVeriIslemeIDDondur(DataSet ds)
    {
        string sIDFieldName = ds.Tables[0].TableName.Replace("dbo.", "") + "ID";
        TopluVeriIslemeIDDondur(ds, sIDFieldName);
    }
    public void TopluVeriIslemeIDDondur(DataTable dt)
    {
        string sIDFieldName = dt.TableName + "ID";
        TopluVeriIslemeIDDondur(dt, sIDFieldName);
    }
    public void TopluVeriIslemeIDDondur(DataSet ds, string sIDFieldName)
    {
        string ssql = "SELECT * FROM " + ds.Tables[0].TableName;
        TopluVeriIslemeIDDondur(ds.Tables[0], ssql, sIDFieldName);
    }
    public void TopluVeriIslemeIDDondur(DataTable dt, string sIDFieldName)
    {
        string ssql = "SELECT * FROM " + dt.TableName;
        TopluVeriIslemeIDDondur(dt, ssql, sIDFieldName);
    }

    public int TopluVeriIslemeIDDondur(DataTable dt, string ssql, string sIDFieldName)
    {
        int functionReturnValue = 0;

        SqlDataAdapter adp = new SqlDataAdapter(ssql, SqlBaglantisi);
        SqlDataAdapter adptmp = new SqlDataAdapter(ssql, SqlBaglantisi);

        if (TransactionalMi)
            adp.SelectCommand.Transaction = Transaction;
        try
        {
            SqlCommandBuilder cb = new SqlCommandBuilder(adp);
            SqlCommand cmd;
            cmd = cb.GetInsertCommand();
            cmd.CommandText += "; " + ssql + " Where " + sIDFieldName + "=@@IDENTITY";
            cmd.UpdatedRowSource = UpdateRowSource.Both;
            adptmp.InsertCommand = cmd;
            adptmp.UpdateCommand = cb.GetUpdateCommand();
            adptmp.DeleteCommand = cb.GetDeleteCommand();

            adptmp.Update(dt);
            LogWriteDataTable(dt);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            BaglantiyiKapat();
        }
        return functionReturnValue;
    }

    public IDbDataParameter CreateParameter(string name, DbType dbType, object value)
    {
        System.Data.SqlClient.SqlParameter oSqlParameter = new System.Data.SqlClient.SqlParameter();
        oSqlParameter.ParameterName = "@" + name;
        oSqlParameter.DbType = dbType;
        oSqlParameter.Value = value;
        return oSqlParameter;
    }

    public IDbDataParameter CreateParameter(string name, DbType dbType, object value, int size)
    {
        System.Data.SqlClient.SqlParameter oSqlParameter = new System.Data.SqlClient.SqlParameter();
        oSqlParameter.ParameterName = "@" + name;
        oSqlParameter.DbType = dbType;
        oSqlParameter.Value = value;
        oSqlParameter.Size = size;
        return oSqlParameter;
    }

    public string DbConcat()
    {
        return "+";
    }
}